package godis

import (
	"fmt"
	"time"
)

const Serializer = "json"

type DBGetFunc func() string

type SimpleCache struct {
	Operation  *StringOperation
	Expire     time.Duration
	DBGetter   DBGetFunc
	Serializer string
	Policy     CachePolicy
}

func NewSimpleCache(operation *StringOperation, expire time.Duration, serializer string, policy CachePolicy) *SimpleCache {

	policy.SetOperation(operation)
	return &SimpleCache{Operation: operation, Expire: expire, Serializer: serializer, Policy: policy}
}

func (self *SimpleCache) SetCache(key string, value interface{}) {
	self.Operation.Set(key, value, WithExpire(self.Expire))
}

func (self *SimpleCache) GetCache(key string) (ret interface{}) {

	if self.Policy != nil {
		self.Policy.Before(key)
	}

	//解析模式 比如json
	if self.Serializer == Serializer {

		ret = self.Operation.Get(key).UnWarpOrElse(self.DBGetter)
	}

	fmt.Println("------0", ret)

	return ret
}
